<template>
  <div class="header">
    <!--头顶部开始-->
    <div class="nav-topbar">
      <div class="container">
        <div class="topbar-menu">
          <a href="javascript:">小米商城</a>
          <a href="javascript:">MUI</a>
          <a href="javascript:">loT</a>
          <a href="javascript:">云服务</a>
          <a href="javascript:">协议规则</a>
        </div>
        <div class="topbar-user">
          <a href="javascript:" v-if="username">{{ username }}</a>
          <a href="javascript:" v-if="username" @click="logout">退出</a>
          <a href="javascript:" v-if="!username" @click="login">登录</a>
          <a href="javascript:" v-if="!username">注册</a>
          <a href="/order/list" v-if="username">我的订单</a>
          <a href="javascript:" class="my-cart" @click="goToCart"><span
            class="icon-cart"></span>购物车({{ cartCount }})</a>
        </div>
      </div>
    </div>
    <!--头顶部结束-->
    <!--头底部开始-->
    <div class="nav-header">
      <div class="container">
        <div class="header-logo">
          <a href="/"></a>
        </div>
        <div class="header-menu">
          <div class="item-menu">
            <span>小米手机</span>
            <div class="children">
              <ul>
                <li class="product" v-for="item in phoneList" :key="item.id">
                  <a :href="'/product/'+item.id" target="_blank">
                    <div class="pro-img"><img v-lazy="item.mainImage" :alt="item.subtitle"></div>
                    <div class="pro-name">{{ item.name }}</div>
                    <div class="pro-price">{{ item.price | currency }}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>RedMi红米</span>
            <div class="children"></div>
          </div>
          <div class="item-menu">
            <span>电视</span>
            <div class="children"></div>
          </div>
        </div>
        <div class="header-search">
          <div class="wrapper">
            <input type="text" name="keyword">
            <a href="javascript:"></a>
          </div>
        </div>
      </div>
    </div>
    <!--头底部开始-->

  </div>

</template>

<script>
import {getProductList} from "@/api/product";
import {getCartCount, logout} from "@/api/user";
import {removeToken} from "@/utils/auth";

export default {
  name: "NavHeader",
  data() {
    return {
      user: {
        username: this.$store.state.username,
      },
      //手机列表
      phoneList: [],
    }
  },
  filters: {
    //格式化货币
    currency(val) {
      if (!val) return '0.00';
      return '￥' + val.toFixed(2) + '元';
    }
  },
  computed: {
    username() {
      return this.$store.state.username
    },
    cartCount() {
      return this.$store.state.cartCount
    }
  },
  mounted() {
    this.getProductList();
    if (this.$route.params && this.$route.params.from === 'login') {
      this.getCartCount();
    }
  },
  created() {
  },
  methods: {
    //获取商品列表
    getProductList() {
      getProductList('100012', 1, 6).then(res => {
        if (res.list.length > 6) {
          this.phoneList = res.list.slice(0, 6)
        } else {
          this.phoneList = res.list
        }
      })
    },
    //跳转至登录页面
    login() {
      this.$router.push('/login')
    },
    //跳转至购物车页面
    goToCart() {
      this.$router.push('/cart')
    },
    logout() {
      logout().then(() => {
        this.$message.success("退出成功");
        //清除 cookie
        removeToken();
        this.$store.dispatch('saveUserName', undefined);
        this.$store.dispatch('saveCartCount', 0);
      })
    },
    //获取购物车数量
    getCartCount() {
      getCartCount().then(res => {
        this.$store.dispatch('saveCartCount', res)
      })
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/scss/config.scss";
@import "~@/assets/scss/base.scss";
@import "~@/assets/scss/mixin.scss";

.header {
  .nav-topbar {
    height: 39px;
    line-height: 39px;
    background-color: #333333;
    color: #B0B0B0;

    .container {
      @include flex();
      /*display: flex; !*两边对齐*!
      justify-content: space-between; !*水平居中*!
      align-items: center;*/

      a {
        display: inline-block;
        color: #B0B0B0;
        margin-right: 17px;
      }

      .my-cart {
        width: 110px;
        background-color: #FF6600;
        text-align: center;
        color: #ffffff;

        .icon-cart {
          @include bgImg(16px, 12px, "/imgs/icon-cart-checked.png", contain);
          /*display: inline-block;
          width: 16px;
          height: 12px;
          background: url("/imgs/icon-cart-checked.png") no-repeat center; !*填充 居中*!
          background-size: contain;*/
          margin-right: 4px;
        }
      }
    }
  }

  .nav-header {
    .container {
      position: relative;
      height: 112px;
      @include flex();

      .header-logo {
        display: inline-block;
        width: 55px;
        height: 55px;
        background-color: #FF6600;

        a {
          display: inline-block;
          width: 110px;
          height: 55px;

          &:before {
            content: ' '; /*很重要!!!,占位用*/
            @include bgImg(55px, 55px, "/imgs/mi-logo.png", 55px);
            /*display: inline-block;
            width: 55px;
            height: 55px;
            background: url("/imgs/mi-logo.png") no-repeat center;
            background-size: 55px;*/
            transition: margin .2s;
          }

          &:after {
            content: ' '; /*很重要*/
            @include bgImg(55px, 55px, "/imgs/mi-home.png", 55px);

            /*display: inline-block;
            width: 55px;
            height: 55px;
            background: url("/imgs/mi-home.png") no-repeat center;
            background-size: 55px;*/
          }

          &:hover:before { /*修改before*/
            margin-left: -55px;
            transition: margin .2s;
          }
        }
      }

      .header-menu {
        display: inline-block;
        width: 643px;
        padding-left: 209px;

        .item-menu {
          display: inline-block;
          color: #333333;
          font-weight: bold;
          font-size: 16px;
          margin-right: 20px;

          span {
            cursor: pointer;
          }

          &:hover {
            color: $colorA;

            .children {
              height: 220px;
              opacity: 1; /*透明的*/
            }
          }

          .children {
            position: absolute;
            top: 112px;
            left: 0;
            width: $min-width;
            height: 0;
            opacity: 0; /*透明的*/
            border-top: 1px solid #e5e5e5;
            box-shadow: 0 7px 6px 0 rgba(0, 0, 0, 0.11); /*阴影*/
            z-index: 10;
            overflow: hidden;
            transition: height .3s;
            background-color: #ffffff;

            .product {
              position: relative;
              float: left;
              width: 16.6%;
              height: 220px;
              font-size: $fontK;
              line-height: $fontK;
              text-align: center;

              a {
                display: inline-block;
              }

              img {
                width: auto;
                height: 111px;
                margin-top: 26px;
              }

              .pro-img {
                height: 137px;
              }

              .pro-name {
                font-weight: bold;
                margin-top: 19px;
                margin-bottom: 8px;
                color: $colorB;
              }

              .pro-price {
                color: $colorA;
              }

              &:before {
                content: ' ';
                position: absolute;
                top: 28px;
                right: 0;
                border-left: 1px solid $colorF;
                height: 100px;
                width: 1px;
              }

              &:last-child:before {
                display: none;
              }
            }

          }
        }
      }

      .header-search {
        width: 319px;

        .wrapper {
          height: 50px;
          border: 1px solid #E0E0E0;
          display: flex;
          align-items: center;

          input {
            border: none;
            box-sizing: border-box; /*盒子模型，使其宽度不包括padding*/
            border-right: 1px solid #E0E0E0;
            width: 264px;
            height: 50px;
            padding-left: 14px;
          }

          a {
            @include bgImg(18px, 18px, "/imgs/icon-search.png");
            /*display: inline-block;
            width: 18px;
            height: 18px;
            background: url("/imgs/icon-search.png") no-repeat center;
            background-size: contain;!*使背景图充满容器*!*/
            /* margin-left: 17px ;*/
            margin-left: auto;
            margin-right: auto;
          }
        }
      }
    }
  }

}

</style>
